package net.minecraft.src;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import net.minecraft.client.Minecraft;

public class EntityTehVillager extends EntityVillager {
	
    public InventoryTehVillager inventory;
    public Container inventorySlots;
    protected boolean followingPlayer = false;
    
    public ItemStack itemInUse;
    public int itemInUseCount;
    public int nextAttack = 0;
   
    
    protected int entityID;
	
	public EntityTehVillager(World world) {
		super(world);
		texture = "/mob/char.png";
		entityID = rand.nextInt();
		
		inventory = new InventoryTehVillager(this);
		
		switch (rand.nextInt() % 4) {
			case 0:
				inventory.setInventorySlotContents(0, new ItemStack(Item.pickaxeSteel));
			break;
			case 1:
				inventory.setInventorySlotContents(0, new ItemStack(Item.axeSteel));
			break;
			case 2:
				inventory.setInventorySlotContents(0, new ItemStack(Item.swordSteel));
			break;
			case 3:
				inventory.setInventorySlotContents(0, new ItemStack(Item.shovelSteel));
			break;
		}
	}
	
	@Override
	public int getMaxHealth() {
		return 1;
	}
	
	@Override
	public void onEntityUpdate() {
		super.onEntityUpdate();
		
		if (itemInUse != null) {
            ItemStack itemstack = inventory.getCurrentItem();
            if (itemstack != itemInUse)
                clearItemInUse();
            else
            	itemInUse.getItem().onUsingItemTick(itemInUse, null, itemInUseCount);
        }
		
		try {
			followingPlayer = (ModLoader.getMinecraftInstance().thePlayer.inventory.getCurrentItem().getItem() == Item.beefCooked);
		} catch (NullPointerException ex) {
			followingPlayer = false;
		}
		
		
		if (followingPlayer)
			followPlayer();
		else {
			setTarget(null);
			String item;
			
			try {
				item = getEquippedItem().getItemName();
			} catch (NullPointerException ex) {
				item = "";
			}
			//worldObj.getClosestPlayerToEntity(this, -1d).addChatMessage(item);
			
			if (item.contains("pickaxe") && !isJumping) {
				setJumping(true);
			} else if (item.contains("sword")) {
				AxisAlignedBB bounds = AxisAlignedBB.getBoundingBox(posX - 10, posY - 4, posZ - 10, posX + 10, posY + 4, posZ + 10);
				List targets = worldObj.getEntitiesWithinAABBExcludingEntity(this, bounds);
				EntityLiving target = null;
				try {
					target = (EntityLiving)targets.get(0);
				} catch (IndexOutOfBoundsException ex) {
					target = null;					
				} catch (ClassCastException ex) {
					target = null;
				}
				
				if (target != null && getDistanceSqToEntity(target) < 9) {
					if (nextAttack > 0)
						nextAttack--;
					else {
						faceEntity(target, 100f, 100f);
						target.attackEntityFrom(DamageSource.causeMobDamage(this), getEquippedItem().getDamageVsEntity(target));
						nextAttack = 35;
					}
				}
				setTarget(target);
			}
		}
	}
	
	public void followPlayer() {
		EntityPlayer player = worldObj.getClosestPlayerToEntity(this, -1d);
		if (getDistanceSqToEntity(player) > 16)
			setTarget(player);
	}
	
	@Override
	public ItemStack getHeldItem() {
		return inventory.getCurrentItem();
	}
	
	public Item getEquippedItem() {
		return inventory.getCurrentItem().getItem();
	}
	
	public void setHeldItem(int slot) {
		if (slot >= inventory.getSizeInventory())
			throw new IndexOutOfBoundsException();
		inventory.currentSlot = slot;
	}
	
    public void setItemInUse(ItemStack itemstack, int i) {
        if (itemstack == itemInUse)
            return;

        itemInUse = itemstack;
        itemInUseCount = i;
    }
    
    public ItemStack getItemInUse() {
        return itemInUse;
    }

    public int getItemInUseCount() {
        return itemInUseCount;
    }

    public boolean isUsingItem() {
        return itemInUse != null;
    }

    public int getItemInUseDuration() {
        if (isUsingItem())
            return itemInUse.getMaxItemUseDuration() - itemInUseCount;
        else
            return 0;
    }

    public void stopUsingItem() {
        if (itemInUse != null)
            itemInUse.onPlayerStoppedUsing(worldObj, null, itemInUseCount);
        clearItemInUse();
    }

    public void clearItemInUse() {
        itemInUse = null;
        itemInUseCount = 0;
    }
	
    public void writeEntityToNBT(NBTTagCompound saveData)
    {
        super.writeEntityToNBT(saveData);
        NBTTagCompound invData = new NBTTagCompound();
        inventory.writeToNBT(invData);
        saveData.setCompoundTag("inventory", invData);
    }

    public void readEntityFromNBT(NBTTagCompound saveData)
    {
        super.readEntityFromNBT(saveData);
        texture = "/mob/char.png";
        inventory.readFromNBT(saveData.getCompoundTag("inventory"));
        
    }
    
	
}
